import Vue from 'vue';
import Router, { RouteConfig } from 'vue-router';

Vue.use(Router);

import Layout from '@/layout/index.vue';

/** 公共路由 */
export const constantRoutes: Array<RouteConfig> = [
  {
    path: '',
    component: Layout,
    redirect: 'index',
    children: [
      {
        path: 'index',
        component: () => import('@/views/index/index.vue'),
        name: 'Index',
        meta: {
          title: '编程技术分享',
          icon: 'dashboard',
          affix: true,
          content: {
            keywords: '青桔柠檬、前后端分离、博客系统、微服务、lemon、huangpeng、huangpengnet',
            description: '青桔柠檬，一个基于微服务架构的前后端分离博客系统，系统后台分为：网关中心、认证中心、核心系统、代码生成、调度中心、文件管理、监控中心、博客前台、博客后台、消息中心10个微服务。前台分为前台、后台两个项目，使用nginx在服务器上部署，并配置反向代理、负载均衡。'
          }
        }
      },
      {
        path: '/info/:id',
        name: 'ArticleInfo',
        component: () => import('@/views/article/info/index.vue'),
        meta: { title: '文章详情', icon: 'dashboard', affix: true }
      },
      {
        path: '/search',
        name: 'Search',
        component: () => import('@/views/search/index.vue'),
        meta: { title: '文章搜索', icon: 'dashboard', affix: true }
      },
      {
        path: '/userCenter',
        name: 'UserCenter',
        component: () => import('@/views/userCenter/index.vue'),
        meta: { title: '我的主页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/redirect',
    component: Layout,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect.vue')
      }
    ]
  },
  {
    path: '/404',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '页面不存在',
      _blank: true
    }
  },
  {
    path: '/401',
    component: () => import('@/views/error/401.vue'),
    meta: {
      title: '非法访问',
      _blank: true
    }
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/login/index.vue'),
    meta: {
      title: '用户登录',
      _blank: true
    }
  },
  {
    path: '/thirdLogin',
    name: 'ThirdLogin',
    component: () => import('../views/login/thirdLogin.vue'),
    meta: {
      title: '第三方系统用户登录'
    }
  },
  {
    path: '/thirdBind',
    name: 'ThirdBind',
    component: () => import('../views/manager/thirdBind.vue'),
    meta: {
      title: '第三方系统用户绑定'
    }
  }
];

/** 动态路由 */
export const dynamicRoutes = [];

// 防止连续点击多次路由报错
const routerPush = Router.prototype.push;
Router.prototype.push = function push(location) {
  // @ts-ignore
  return routerPush.call(this, location).catch((err: any) => err);
};

export default new Router({
  mode: 'history', // 去掉url中的#
  base: process.env.BASE_URL,
  scrollBehavior(to, from, savedPosition) {
    return { x: 0, y: 0 };
  },
  routes: constantRoutes
});

